﻿
namespace RIA.ServicesLibrary.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using RIA.ServicesLibrary.Web.Models;


    // Implements application logic using the AdventureWorksLT_DataEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess ()]
    public class CustomerService : LinqToEntitiesDomainService<AdventureWorksLT_DataEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Addresses' query.
        public IQueryable<Address> GetAddresses ()
        {
            return this.ObjectContext.Addresses;
        }

        
        public void InsertAddress ( Address address )
        {
            if ( ( address.EntityState != EntityState.Detached ) )
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState ( address, EntityState.Added );
            }
            else
            {
                this.ObjectContext.Addresses.AddObject ( address );
            }
        }

        public void UpdateAddress ( Address currentAddress )
        {
            this.ObjectContext.Addresses.AttachAsModified ( currentAddress, this.ChangeSet.GetOriginal ( currentAddress ) );
        }

        public void DeleteAddress ( Address address )
        {
            if ( ( address.EntityState == EntityState.Detached ) )
            {
                this.ObjectContext.Addresses.Attach ( address );
            }
            this.ObjectContext.Addresses.DeleteObject ( address );
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Customers' query.
        [Query(IsComposable=true)]
        public IQueryable<Customer> GetCustomers ()
        {
            return this.ObjectContext.Customers.Include ( "CustomerAddresses.Address" );
        }

        public IQueryable<Customer> GetPagedCustomers ()
        {
            return this.ObjectContext.Customers.OrderBy ( x => x.LastName );
        }
        
        public Customer GetCustomer ( int customerID )
        {
            var qry=  this.ObjectContext
                .Customers.Include("CustomerAddresses.Address")
                .Where(c=>c.CustomerID == customerID);
            Customer customer = qry.FirstOrDefault ();
            return customer;
            
        }
        public void InsertCustomer ( Customer customer )
        {
            if ( ( customer.EntityState != EntityState.Detached ) )
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState ( customer, EntityState.Added );
            }
            else
            {
                this.ObjectContext.Customers.AddObject ( customer );
            }
        }

        public void UpdateCustomer ( Customer currentCustomer )
        {
            this.ObjectContext.Customers.AttachAsModified ( currentCustomer, this.ChangeSet.GetOriginal ( currentCustomer ) );
        }

        public void DeleteCustomer ( Customer customer )
        {
            if ( ( customer.EntityState == EntityState.Detached ) )
            {
                this.ObjectContext.Customers.Attach ( customer );
            }
            this.ObjectContext.Customers.DeleteObject ( customer );
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'CustomerAddresses' query.
        public IQueryable<CustomerAddress> GetCustomerAddresses ()
        {
            return this.ObjectContext.CustomerAddresses;
        }

        public void InsertCustomerAddress ( CustomerAddress customerAddress )
        {
            if ( ( customerAddress.EntityState != EntityState.Detached ) )
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState ( customerAddress, EntityState.Added );
            }
            else
            {
                this.ObjectContext.CustomerAddresses.AddObject ( customerAddress );
            }
        }

        public void UpdateCustomerAddress ( CustomerAddress currentCustomerAddress )
        {
            this.ObjectContext.CustomerAddresses.AttachAsModified ( currentCustomerAddress, this.ChangeSet.GetOriginal ( currentCustomerAddress ) );
        }

        public void DeleteCustomerAddress ( CustomerAddress customerAddress )
        {
            if ( ( customerAddress.EntityState == EntityState.Detached ) )
            {
                this.ObjectContext.CustomerAddresses.Attach ( customerAddress );
            }
            this.ObjectContext.CustomerAddresses.DeleteObject ( customerAddress );
        }
    }
}


